package com.auto.survey.activity.monitoring.entity.ctrlmeasure

import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import com.auto.survey.activity.monitoring.entity.MonitoringPoint
import com.auto.survey.util.Helper

/**
 * 闭合观测序列中的观测点数据类
 */
@Entity(
    tableName = "LoopObservationPoint",
    foreignKeys = [
        ForeignKey(
            entity = ClosedLoopObservation::class,
            parentColumns = ["id"],
            childColumns = ["loopId"],
            onDelete = ForeignKey.CASCADE
        ),
        // 关联监测点
        ForeignKey(
            entity = MonitoringPoint::class,
            parentColumns = ["id"],
            childColumns = ["pointId"],
            onDelete = ForeignKey.CASCADE // 监测点删除时，删除关联的记录
        )
    ],
    indices = [Index(value = ["loopId"]),
        Index(value = ["pointId"]),
        Index(value = ["loopId", "orderInLoop"], unique = true)// 确保序列内顺序唯一
    ]
)
data class LoopObservationPoint(
    /**
     * ID，主键，自增长
     */
    @PrimaryKey(autoGenerate = true) val id: Int = 0,

    /**
     * 所属闭合序列ID
     */
    val loopId: Int,

    /**
     * 测点ID
     */
    val pointId: Int,

    /**
     * 观测点在序列中的顺序
     */
    val orderInLoop: Int,

    /**
     * 观测时间
     */
    val observationTime: String = Helper.formatTimestamp(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"),

    /**
     * 关联的观测数据ID
     */
    val observationDataId: Int? = null
)
